package com.ttai.untils.getui;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

//import com.Tools.CommonUtil;
//import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.message.*;
import com.ttai.model.net.GeiTuiBean;
import com.ttai.ui.activity.IpError;
import com.ttai.ui.activity.ZhiWen;
import com.ttai.untils.Constant;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 继承 GTIntentService 接收来自个推的消息, 所有消息在线程中回调, 如果注册了该服务, 则务必要在 AndroidManifest中声明, 否则无法接受消息<br>
 * onReceiveMessageData 处理透传消息<br>
 * onReceiveClientId 接收 cid <br>
 * onReceiveOnlineState cid 离线上线通知 <br>
 * onReceiveCommandResult 各种事件处理回执 <br>
 */
public class DemoIntentService extends GTIntentService {

    private String buildtime;

    public DemoIntentService() {

    }

    @Override
    public void onReceiveServicePid(Context context, int pid) {
    }

    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage msg) {
        String appid = msg.getAppid();
        String taskid = msg.getTaskId();
        String messageid = msg.getMessageId();
        byte[] payload = msg.getPayload();
        String pkg = msg.getPkgName();
        String cid = msg.getClientId();
        String info = new String(payload);
        System.out.println("info:"+info);
        Gson gson = new Gson();
        GeiTuiBean geiTuiBean = gson.fromJson(info,GeiTuiBean.class);
        String productName = geiTuiBean.getProductName();
        String buildtime = geiTuiBean.getTime();
        String appId = geiTuiBean.getAppid();
        String flag = geiTuiBean.getFlag();

        String time= Constant.timeStampToDateStr((buildtime),Constant.PATTERN_FULL);
            if(buildtime.equals("200")){
                String ip = geiTuiBean.getIp();
                String appName = geiTuiBean.getProductName();
                System.out.println("ip:"+ip);
                System.out.println("appName:"+appName);
                System.out.println("appId:"+appId);
                System.out.println("flag:"+flag);
                Intent intent1 = new Intent(context,IpError.class);
                intent1.putExtra("ip",ip);
                intent1.putExtra("appName",appName);
                intent1.putExtra("appId",appId);
                intent1.putExtra("flag",flag);
                context.startActivity(intent1);
            }else {
                System.out.println("productName :"+productName+"buildtime:"+time);

                Intent intent=new Intent();
                intent.putExtra("productName",productName);
                intent.putExtra("time",time);
                intent.setClass(DemoIntentService.this,ZhiWen.class);
                context.startActivity(intent);
            }
    }

    @Override
    public void onReceiveClientId(Context context, String clientid) {
        Log.i(TAG, "onReceiveClientId -> " + "clientid = " + clientid);
    }

    @Override
    public void onReceiveOnlineState(Context context, boolean online) {
    }

    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage cmdMessage) {
    }

    @Override
    public void onNotificationMessageArrived(Context context, GTNotificationMessage msg) {
    }

    @Override
    public void onNotificationMessageClicked(Context context, GTNotificationMessage msg) {
    }
}